package me.heyclock.hl.recordwriter.adapter;

import android.content.Context;
import android.graphics.Color;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.heyclock.hl.recordwriter.R;
import me.heyclock.hl.recordwriter.bean.HomeFunctionBean;
import me.heyclock.hl.recordwriter.tools.HC_Screen;
import me.heyclock.hl.recordwriter.ui.GlideApp;

public class HomeFunctionAdapter extends RecyclerView.Adapter<HomeFunctionAdapter.HomeFunctionViewHoleder>{

    private WeakReference<Context> mContext;
    private List<HomeFunctionBean> homeFunctionBeanList;
    private HashMap<HomeFunctionViewHoleder, Integer> mListHolder ;
    private OnItemClickListener<HomeFunctionBean> mOnItemClickListener = null;

    public HomeFunctionAdapter(Context context, List<HomeFunctionBean> homeFunctionBeanList){
        this.mContext = new WeakReference<>(context);
        this.homeFunctionBeanList = homeFunctionBeanList;
        this.mListHolder = new HashMap<HomeFunctionViewHoleder, Integer>();
    }

    @NonNull
    @Override
    public HomeFunctionAdapter.HomeFunctionViewHoleder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext.get()).inflate(R.layout.activity_function_item, parent, false);
        return new HomeFunctionViewHoleder(mContext.get(), view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeFunctionAdapter.HomeFunctionViewHoleder holder, final int position) {
        holder.bindData(homeFunctionBeanList.get(position));
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onClick(v, homeFunctionBeanList.get(position));
            }
        });
        ///< 存储Holder，后面做监听释放处理【容错处理，如果添加过则跳过】
        if (!mListHolder.containsKey(holder)){
            mListHolder.put(holder, position);
        }
    }

    @Override
    public int getItemCount() {
        return ((null == homeFunctionBeanList) ? 0 : homeFunctionBeanList.size());
    }

    /**
     * 外界进行调用该方法，为item设置点击事件；重要
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    /**
     * 取消监听，避免内存泄漏
     */
    public void onDestroy(){
        if (null != mListHolder) {
            for (HomeFunctionViewHoleder key : mListHolder.keySet()) {
                key.root.setOnClickListener(null);
            }
            mListHolder.clear();
            mListHolder = null;
        }
    }

    /**
     * 数据持有
     */
    static class HomeFunctionViewHoleder extends RecyclerView.ViewHolder{

        private Context context;
        private View itemView;
        @BindView(R.id.afi_root)
        public ConstraintLayout root;
        @BindView(R.id.afi_foreImage)
        public ImageView foreImage;
        @BindView(R.id.afi_tip)
        public TextView tip;

        public HomeFunctionViewHoleder(Context context, View itemView) {
            super(itemView);
            this.context = context;
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }

        public void bindData(HomeFunctionBean data){
            ///< -10000用于空布局，这样可以实现两列的情况下 - 一个条目同时占据两列
            if (-10000 != data.getImgW() && -10000 != data.getImgH()){
                root.setVisibility(View.VISIBLE);
                root.setBackgroundColor(data.getBackColor());
                HC_Screen.setViewGroupWH(root, data.getImgW(), data.getImgH());
                HC_Screen.setConstraintLayoutWH(foreImage, data.getImgW()/2, data.getImgH()/2);
                GlideApp.with(context).load(data.getFunctionForImg())
                        .centerInside()
                        //.placeholder(R.mipmap.ic_launcher)
                        .into(foreImage);
                tip.setText(data.getFunctionName());
            }
            else{
                HC_Screen.setViewGroupWH(root, HC_Screen.SCREEN_WIDTH/2, HC_Screen.SCREEN_WIDTH/2);
                root.setVisibility(View.INVISIBLE);
            }
        }
    }
}
